package com.lc.advertising.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.advertising.R;
import com.lc.advertising.app.AppActivity;
import com.lc.advertising.http.request.GetCodeApi;
import com.lc.advertising.http.request.RegisterApi;
import com.lc.advertising.http.response.RegisterBean;
import com.lc.advertising.manager.InputTextManager;
import com.lc.advertising.util.CourseUtils;
import com.lc.advertising.widget.CountdownView;
import com.lc.advertising.widget.PasswordEditText;
import com.lc.advertising.widget.RegexEditText;

import androidx.appcompat.widget.AppCompatEditText;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends AppActivity {
    @BindView(R.id.et_register_phone)
    RegexEditText etRegisterPhone;
    @BindView(R.id.et_register_code)
    AppCompatEditText etRegisterCode;
    @BindView(R.id.cv_register_countdown)
    CountdownView cvRegisterCountdown;
    @BindView(R.id.et_register_password1)
    PasswordEditText etRegisterPassword1;
    @BindView(R.id.et_register_password2)
    PasswordEditText etRegisterPassword2;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.rl_code)
    RelativeLayout rlCode;
    @BindView(R.id.ll_password1)
    LinearLayout llPassword1;
    @BindView(R.id.ll_password2)
    LinearLayout llPassword2;
    @BindView(R.id.btn_login_commit)
    Button btnLoginCommit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        InputTextManager.with(this)
                .addView(etRegisterPhone)
                .addView(etRegisterCode)
                .addView(etRegisterPassword1)
                .addView(etRegisterPassword2)
                .setMain(btnLoginCommit)
                .build();
        etRegisterPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    llPhone.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                    rlCode.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                }
            }
        });
        etRegisterCode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    llPhone.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    rlCode.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                    llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                }
            }
        });
        etRegisterPassword1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    llPhone.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    rlCode.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                    llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                }
            }
        });
        etRegisterPassword2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    llPhone.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    rlCode.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                }

            }
        });
    }


    @OnClick({R.id.image_back, R.id.ll_phone, R.id.cv_register_countdown, R.id.rl_code, R.id.ll_password1, R.id.tv_xieyi, R.id.btn_login_commit, R.id.ll_password2, R.id.et_register_phone, R.id.et_register_code, R.id.et_register_password1, R.id.et_register_password2})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.image_back:
//
                finish();
                break;
            case R.id.ll_phone:
                llPhone.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                rlCode.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                break;
            case R.id.cv_register_countdown:
                if (CourseUtils.isNullOrEmpty(etRegisterPhone.getText().toString().trim())){
                    toast("请输入手机号");
                    return;
                }
                    EasyHttp.post(this)
                            .api(new GetCodeApi()
                                    .setMobile(etRegisterPhone.getText().toString()))
                            .request(new HttpCallback<RegisterBean>(this) {

                                @Override
                                public void onSucceed(RegisterBean data) {
                                    toast(data.getMessage());
                                    if (data.getCode()==200){
                                        cvRegisterCountdown.start();
                                    }

                                }

                                @Override
                                public void onFail(Exception e) {
                                    super.onFail(e);
                                    cvRegisterCountdown.start();
                                }
                            });
                llPhone.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                rlCode.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);

                break;
            case R.id.rl_code:
                llPhone.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                rlCode.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                break;
            case R.id.ll_password1:
                llPhone.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                rlCode.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                break;
            case R.id.ll_password2:
                llPhone.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                rlCode.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                break;
            case R.id.tv_xieyi:
                startActivity(new Intent(this,DanyeActivity.class).putExtra("id","32").putExtra("title","注册协议"));
                break;
            case R.id.btn_login_commit:
                register();
                break;
        }
    }

    private void register() {
        if (!etRegisterPassword1.getText().toString().trim().equals(etRegisterPassword2.getText().toString().trim())){
            toast("两次输入密码不一致");
        }
        // 提交注册
        EasyHttp.post(this)
                .api(new RegisterApi()
                        .setMobile(etRegisterPhone.getText().toString())
                        .setYzm(etRegisterCode.getText().toString())
                        .setPassword(etRegisterPassword1.getText().toString())
                        .setPassword2(etRegisterPassword2.getText().toString()))
                .request(new HttpCallback<RegisterBean>(this) {


                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMessage());
                         if (data.getCode()==200){
                             finish();
                         }
                    }


                });
    }

}
